﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MeizuSDK.Core
{
    /// <summary>
    /// 重力感应传感器类
    /// </summary>
    public sealed class GravitySensor : IDisposable
    {
        #region DllImport
        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool AccAPIOpen();

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern void AccAPIClose();

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool AccAPIGetXYZ(out sbyte x_axis, out sbyte y_axis, out sbyte z_axis);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool AccAPIGetX(out sbyte x_axis);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool AccAPIGetY(out sbyte y_axis);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool AccAPIGetZ(out sbyte z_axis);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern uint AccAPIGetMessage();
        #endregion

        #region 私有静态变量
        /// <summary>
        /// 单例变量
        /// </summary>
        private static GravitySensor instance;
        /// <summary>
        /// 单例同步对象
        /// </summary>
        private static object SyncRoot = new object();

        /// <summary>
        /// 用来标识重力加速度传感器是否已经打开
        /// </summary>
        private static bool isOpened;
        #endregion

        #region 公共静态属性
        /// <summary>
        /// 获取重力感应传感器实例
        /// </summary>
        public static GravitySensor Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (SyncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new GravitySensor();
                        }
                    }
                }

                return instance;
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造重力感应传感器实例
        /// </summary>
        private GravitySensor()
        {
            Open();
        }
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取重力转屏消息值
        /// </summary>
        public uint Message
        {
            get
            {
                return AccAPIGetMessage();
            }
        }
        /// <summary>
        /// 获取传感器数据
        /// </summary>
        public GravityData Data
        {
            get
            {
                sbyte x, y, z;

                AccAPIGetXYZ(out x, out y, out z);

                return new GravityData() { X = x, Y = y, Z = z };
            }
        }
        /// <summary>
        /// 获取X轴加速度值
        /// </summary>
        public int X
        {
            get
            {
                sbyte x;

                AccAPIGetX(out x);

                return x;
            }
        }
        /// <summary>
        /// 获取Y轴加速度值
        /// </summary>
        public int Y
        {
            get
            {
                sbyte y;

                AccAPIGetY(out y);

                return y;
            }
        }
        /// <summary>
        /// 获取Z轴加速度值
        /// </summary>
        public int Z
        {
            get
            {
                sbyte z;

                AccAPIGetZ(out z);

                return z;
            }
        }
        #endregion

        #region 函数
        /// <summary>
        /// 打开重力加速度传感器
        /// </summary>
        public void Open()
        {
            if (isOpened)
                return;
            isOpened = true;
            AccAPIOpen();
        }
        /// <summary>
        /// 关闭重力加速度传感器
        /// </summary>
        public void Close()
        {
            if (isOpened)
            {
                isOpened = false;
                AccAPIClose();
            }
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            Close();
        }
        #endregion
    }

    /// <summary>
    /// 重力感应数据
    /// </summary>
    public class GravityData
    {
        /// <summary>
        /// X轴加速度值
        /// </summary>
        public int X;
        /// <summary>
        /// Y轴加速度值
        /// </summary>
        public int Y;
        /// <summary>
        /// Z轴加速度值
        /// </summary>
        public int Z;
    }
}
